package com.qcu.controller.admin;

import com.qcu.annotation.LimitRate;
import com.qcu.annotation.RequireAuthorization;
import com.qcu.entity.DoctorMenu;
import com.qcu.exception.RepeatException;
import com.qcu.service.DoctorMenuService;
import com.qcu.util.Response;
import com.qcu.util.ResponseCode;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;


/**
 * @Name: MyComputer
 * @ClassName DoctorMenuController
 * @Description:
 * @date 2025/2/12 9:17
 */
@RestController
@RequestMapping("/admin/doctorMenu")
public class DoctorMenuController {

    @Resource
    private DoctorMenuService doctorMenuService;

    /**
     * 获取所有体检菜单
     * @return
     */
    @PostMapping("/save")
    @RequireAuthorization({"ADMIN"})
    public Response save(@RequestBody DoctorMenu doctorMenu){
        try {
            boolean save = doctorMenuService.save(doctorMenu);
            return save ? Response.success() : Response.failure(ResponseCode.FAILURE, "保存失败");
        } catch (Exception e) {
            throw new RepeatException(e.getMessage());
        }

    }

    /**
     * 修改体检菜单
     * @return
     */
    @PutMapping("/update")
    @RequireAuthorization({"ADMIN"})
    public Response update(@RequestBody DoctorMenu doctorMenu){
        doctorMenu.setUpdatedAt(null);
        boolean update = doctorMenuService.updateById(doctorMenu);
        return update ? Response.success() : Response.failure(ResponseCode.FAILURE, "更新失败");
    }

    /**
     * 删除体检菜单
     * @return
     */
    @DeleteMapping("/remove/{id}")
    @RequireAuthorization({"ADMIN"})
    public Response remove(@PathVariable Integer id){
        boolean remove = doctorMenuService.removeById(id);
        return remove ? Response.success() : Response.failure(ResponseCode.FAILURE, "删除失败");
    }

    /**
     * 获取体检菜单
     * @return
     */
    @GetMapping("/get")
    @RequireAuthorization({"ADMIN","USER"})
    @LimitRate(key = "getDoctorMenu",permitsPerSecond = 2,expire = 10,msg = "八嘎雅鹿，你滴点那么快滴干什么滴干活...")
    public Response get(){
        return Response.success(doctorMenuService.list());
    }

}
